/*
 * This servlet prepares the file list for the manage lesson files
 */
package controllers.classes;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.FileDao;
import dao.LessonDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Files;
import models.Lesson;

/**
 *
 * @author Achie
 */
public class FileController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if(request.getParameter("id")!=null) { //if the lessonid is set
            long lessonid = Long.parseLong(request.getParameter("id"));
            
            HibernateUtil hu = new HibernateUtil();
            
            FileDao fd = new FileDao(hu.openSession());
            List<Files> files = fd.findAllFilesInLesson(lessonid); //get all the file objects
            
            LessonDao ld = new LessonDao(hu.openSession());
            Lesson lesson = ld.get(lessonid); //get the specific lesson of these files
            
            hu.close(); //close connection
            
            //prepare neccessary attributes
            request.setAttribute("file", files);
            request.setAttribute("lessonid", lessonid);
            request.setAttribute("lesson", lesson);
            
            // Breadcrumbs
            Breadcrumbs[] breadcrumbs = {
                new Breadcrumbs("Index", "Home"),
                new Breadcrumbs("CourseLessonEdit", "Manage lessons"),
                new Breadcrumbs("FileController/view?id=" + lessonid, "Manage files")
            };
            request.setAttribute("breadcrumbs", breadcrumbs);
           
            //redirect to the manage files page
            RequestDispatcher dispatcher = request.getRequestDispatcher("../jsp/manage_files.jsp");
            dispatcher.forward(request, response);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
